#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: zhipeng time: 2018/8/14
import pygame
import time
import random


class PlayerPlane(object):

    def __init__(self, screen):
        planeImageName = "./images/hero.gif"

        self.image = pygame.image.load(planeImageName).convert()
        self.x = 190
        self.y = 600
        self.screen = screen
        self.name = "player"
        self.bulletList = []

    def drawPlane(self):
        self.screen.blit(self.image, (self.x, self.y))
        for temp in self.bulletList:
            temp.drawBullte()

    def move(self, keyValue):
        if keyValue == "left" and self.x >= 0:
            self.x -= 20
        elif keyValue == "right":
            self.x += 20
        elif keyValue == "up" and self.y >= 0:
            self.y -= 20
        elif keyValue == "down":
            self.y += 20
        elif keyValue == "space":
            self.bulletList.append(Bullet("player", self.x, self.y))


class EnemyPlane(object):
    def __init__(self, screen, x=0, y=0):
        enemyPlaneFile = "./images/enemy-1.gif"
        self.enemyImage = pygame.image.load(enemyPlaneFile).convert()
        self.x = x
        self.y = y
        self.screen = screen
        self.direction = "right"
        self.name = "enemy"
        self.bulletList = []

    def drawEnemyPlane(self):
        screen.blit(self.enemyImage, (self.x, self.y))
        if self.direction == "right":
            self.x += 2
        elif self.direction == "left":
            self.x -= 2
        if self.x > 425:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"
        randomNum = random.randint(50, 150)
        if randomNum == 50:
            self.bulletList.append(Bullet(self.name, self.x-20, self.y+40))
        for temp in self.bulletList:
            temp.drawBullte()


class Bullet(object):
    def __init__(self, planName, x, y):
        self.planName = planName
        if planName == "enemy":
            bulletImageFile = "./images/bullet-1.gif"
        elif planName == "player":
            #bulletImageFile = "./images/bomb.png"
            bulletImageFile = "./images/bullet-3.gif"

        self.bulletImage = pygame.image.load(bulletImageFile).convert()
        self.x = x
        self.y = y

    def drawBullte(self):
        if self.planName == "enemy":
            self.y += 4
        elif self.planName == "player":
            self.y -= 6
        screen.blit(self.bulletImage, (self.x+40, self.y-15))


if __name__ == "__main__":
    screen = pygame.display.set_mode((480, 790), 0, 0)
    bgImageFile = "./images/background.png"
    background = pygame.image.load(bgImageFile).convert()

    player = PlayerPlane(screen)
    enemy = EnemyPlane(screen)
    while True:
        screen.blit(background, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("exit")
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player.move("left")
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player.move("right")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    player.move("up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player.move("down")
                if event.key == pygame.K_SPACE:
                    player.move("space")

        player.drawPlane()
        enemy.drawEnemyPlane()
        pygame.display.update()
        time.sleep(0.01)
